﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BGSpaceShooter;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ParticleSystem.ParticleSystem;

namespace ParticleSystem
{
    public class FireSmokeEffect : IObjectEffect
    {
        private bool isActive;
        public GameObject GameObject { get; set; }
        public StarFighterComponent Component { get; set; }

        public ParticleEmitter particleEmmiter;


        public FireSmokeEffect(GameObject gameObject, StarFighterComponent component)
        {
            GameObject = gameObject;
            Component = component;
            CreateParticleEmitter();
        }

        private void CreateParticleEmitter()
        {
            particleEmmiter = new ParticleEmitter();

            IsActive = false;

            particleEmmiter.MaxParticles = 150;

            particleEmmiter.Duration = TimeSpan.FromSeconds(1);
            particleEmmiter.BlendState = BlendState.NonPremultiplied;

            particleEmmiter.MinHorizontalVelocity = 0;
            particleEmmiter.MaxHorizontalVelocity = 3;

            particleEmmiter.MinVerticalVelocity = -5;
            particleEmmiter.MaxVerticalVelocity = 5;

            particleEmmiter.Gravity = new Vector3(0, 0, 0);

            particleEmmiter.EndVelocity = 0.75f;

            particleEmmiter.MinRotateSpeed = 0;
            particleEmmiter.MaxRotateSpeed = 0;

            particleEmmiter.MinStartSize = 1;
            particleEmmiter.MaxStartSize = 3;

            particleEmmiter.MinEndSize = 2;
            particleEmmiter.MaxEndSize = 3;


            particleEmmiter.Reset();

            particleEmmiter.LoadContent(Display.GraphicsDevice);
            particleEmmiter.LoadParticleEffect(GameContentManager.Content.GetTexture("smoke"));
        }

        #region IObjectEffect Members

        public bool IsActive
        {
            get { return isActive; }
            set
            {
                isActive = value;
                particleEmmiter.Reset();
            }
        }

        public bool ShouldVibrate()
        {
            return false;
        }

        public EffectDrawer GetDrawer()
        {
            if (GameObject == null)
            {
                throw new NullReferenceException("No GameObject assigned");
            }
            return new FireSmokeEffectDrawer(this, GameObject);
        }

        public void Update(GameTime gameTime)
        {
            if (GameObject == null)
            {
                throw new NullReferenceException("No GameObject assigned");
            }
            Vector3 enginesPosition =
                Vector3.Transform(Component.BoundingSphere.Center, GameObject.AdditionalTransformations * GameObject.RotationMatrix) + GameObject.Position;
            for (int i = 0; i < 5 * 1 - Component.HealthPercent; i++)
                if (this.IsActive)
                {
                    Vector3 speed = Vector3.Transform(new Vector3(0, 0, -5), GameObject.RotationMatrix);
                    particleEmmiter.AddParticle(enginesPosition, speed);

                }
            particleEmmiter.Update(gameTime);
        }

        #endregion
    }
}
